#ifndef ELFSTRUCTS_H
#define ELFSTRUCTS_H

#include <cstdint>
#include <QString>
#include <QVector>


#define STT_NOTYPE 0
#define STT_OBJECT 1
#define STT_FUNC 2
#define STT_SECTION 3
#define STT_FILE 4
#define STT_COMMON 5
#define STT_TLS 6
#define ELF_ST_BIND(x) ((x) >> 4)
#define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf)
#define ELF32_ST_BIND(x) ELF_ST_BIND(x)
#define ELF32_ST_TYPE(x) ELF_ST_TYPE(x)
#define STB_LOCAL 0
#define STB_GLOBAL 1
#define STB_WEAK 2
#define ELF32_ST_VISIBILITY(o)	((o) & 0x03)
#define ELF64_ST_VISIBILITY(o)	ELF32_ST_VISIBILITY (o)
#define STV_DEFAULT	0
#define STV_INTERNAL	1
#define STV_HIDDEN	2
#define STV_PROTECTED	3



// ELF 文件类型定义
enum class ElfClass {
    ELFCLASS32 = 1,
    ELFCLASS64 = 2
};

enum class ElfData {
    ELFDATA2LSB = 1,
    ELFDATA2MSB = 2
};

// ELF Header 结构
struct ElfHeader {
    uint8_t magic[16];
    uint16_t type;
    uint16_t machine;
    uint32_t version;
    uint64_t entry;
    uint64_t phoff;
    uint64_t shoff;
    uint32_t flags;
    uint16_t ehsize;
    uint16_t phentsize;
    uint16_t phnum;
    uint16_t shentsize;
    uint16_t shnum;
    uint16_t shstrndx;
    
    ElfClass elfClass;
    ElfData elfData;
};

// Program Header 结构
struct ProgramHeader {
    uint32_t type;
    uint32_t flags;
    uint64_t offset;
    uint64_t vaddr;
    uint64_t paddr;
    uint64_t filesz;
    uint64_t memsz;
    uint64_t align;
    
    QString typeString() const;
};

// Section Header 结构
struct SectionHeader {
    uint32_t sh_name;
    uint32_t type;
    uint64_t flags;
    uint64_t addr;
    uint64_t offset;
    uint64_t size;
    uint32_t link;
    uint32_t info;
    uint64_t align;
    uint64_t entsize;
    
    QString name;
    QString typeString() const;
};

// 符号表条目
struct SymbolEntry {
    uint32_t name;
    uint8_t info;
    uint8_t other;
    uint16_t shndx;
    uint64_t value;
    uint64_t size;
    
    QString nameStr;
    QString typeString() const;
    QString bindString() const;
    QString visibilityString() const;
};

// 重定位条目
struct RelocationEntry {
    uint64_t offset;
    uint64_t info;
    int64_t addend;
    
    uint32_t symbol;
    uint32_t type;
    QString typeString() const;
};

#endif // ELFSTRUCTS_H
